/*************************************************
* Title：TinyToolKit
* Author：Opportunity 
* Describe：
* CreateTime：2020-08-18 14:07:14
* Version：1.0
* Modify Recorder：
*************************************************/

namespace TinyToolKit.Network
{
    public class HandleRoomMsg : HandleMsgBase
    {
        public HandleRoomMsg(TcpServerHelper server) : base(server)
        {
            _requestType = RequestType.Room;
        }
        
        //创建房间
        public void MsgCreateRoom(Conn conn, ProtocolBase protocol)
        {
            int start = 0;
            ProtocolBytes protBytes= (ProtocolBytes)protocol;
            string request = protBytes.GetString(start, ref start);
            string name = protBytes.GetString(start, ref start);
            int maxPlayerCount = protBytes.GetInt(start, ref start);

            Player player = conn.player;
            ProtocolBytes prot = new ProtocolBytes();
            prot.AddString(request);
            prot.AddString(name);
            //如果玩家当前状态就在房间中，或在战斗中，返回0表示创建失败
            if (player.tempData.status != PlayerTempData.Status.None)
            {
                prot.AddInt(0);
                player.Send(prot);
                return;
            }
            RoomMgr.Instance.CreateRoom(player,maxPlayerCount); 
            prot.AddInt(1);//返回1表示创建成功
            player.Send(prot);
        }

        //加入房间
        public void MsgEnterRoom(Conn conn, ProtocolBase protocol)
        {
            Player player = conn.player;
            
            int start = 0;
            ProtocolBytes prot = (ProtocolBytes) protocol;
            string request = prot.GetString(start, ref start);
            string name = prot.GetString(start, ref start);
            int roomId = prot.GetInt(start, ref start);

            ProtocolBytes newProt = new ProtocolBytes();
            newProt.AddString(request);
            newProt.AddString(name);

            //判断房间是否存在，如果不存在返回0，表示加入失败
            if (RoomMgr.Instance.HasRoom(roomId) == false)
            {
                newProt.AddInt(0);
                player.Send(protocol);
                return;
            }

            Room room = RoomMgr.Instance.GetRoom(roomId);
            //判断当前房间的状态，处于准备中，才可以加入
            if (room.status != Room.Status.Prepare)
            {
                newProt.AddInt(-1);
                player.Send(newProt);
                return;
            }

            //加入成功返回1
            if (room.AddPlayer(player))
            {
                room.Broadcast(room.GetRoomInfo());
                newProt.AddInt(1);
                player.Send(newProt);
            }
            else
            {
                newProt.AddInt(-1);
                player.Send(newProt);
            }
        }
        
        public void MsgLeaveRoom(Conn conn, ProtocolBase protocol)
        {
            Player player = conn.player;
            int start = 0;
            ProtocolBytes prot = (ProtocolBytes) protocol;
            string request = prot.GetString(start, ref start);
            string name = prot.GetString(start, ref start);
            
            ProtocolBytes newProt = new ProtocolBytes();
            newProt.AddString(request);
            newProt.AddString(name);
            
            //如果玩家不在房间中，返回0表示失败
            if (player.tempData.status != PlayerTempData.Status.Room)
            {
                newProt.AddInt(0);
                player.Send(newProt);
                return;
            }

            Room room = player.tempData.room;
            RoomMgr.Instance.LeaveRoom(player);
            room?.Broadcast(room.GetRoomInfo());
            
            //返回1表示离开房间成功
            newProt.AddInt(1);
            player.Send(newProt);
        }

        //获取房间列表
        public void MsgGetRoomList(Conn conn, ProtocolBase protocol)
        {
            conn.player.Send(RoomMgr.Instance.GetRoomList());
        }

        //获取房间信息
        public void MsgGetRoomInfo(Conn conn, ProtocolBase protocol)
        {
            Player player = conn.player;
            //如果当前玩家不在房间内，则返回
            if (player.tempData.status != PlayerTempData.Status.Room) return;

            Room room = player.tempData.room;
            player.Send(room.GetRoomInfo());
        }
    }
}

